package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.part.SearchPartBo;
import com.zhuss.bean.vo.core.part.ListPartVo;
import com.zhuss.common.AreaSelectVo;
import com.zhuss.common.ListVo;
import com.zhuss.common.PageVo;
import com.zhuss.common.SelectVo;
import com.zhuss.core.db.dao.ext.PartDao;
import com.zhuss.core.db.entity.Part;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;


/**
 * 查询区域 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("partRead")
public class PartRead {
	
	@Autowired
	private PartDao partDao;
	
	
	/**
	 * 查询areaName
	 * @return
	 */
	public String areaName(Integer area) {
		List<Part> list = partDao.listAll();
		String areaName = "";
		for (Part item : list) {
			if (item.getArea().equals(area)) {
				areaName = item.getName();
			}
		}
		return areaName;
	}
	
	
	/**
	 * 查询最大的area值
	 * @param 
	 * @return
	 */
	public Integer findMaxAreaIncr() {
		List<Part> result = partDao.listAll();
		Collections.sort(result, new Comparator<Part>() {

			@Override
			public int compare(Part o1, Part o2) {
				return o2.getArea() - o1.getArea();
			}
		});
		Integer area = 0;
		if (result.size() > 0) {
			area = result.get(0).getArea();
		}
		return area + 10;
	}
	
	
	/**
	 * 根据名字查询区域
	 * @param 
	 * @return
	 */
	public List<Part> list(String name) {
		List<Part> result = partDao.listByName(name);
		return result;
	}
	
	public List<Part> listNotId(String name, Long partId) {
		List<Part> result = new ArrayList<Part>();
		List<Part> list = list(name);
		for (Part c : list) {
			if (!c.getPartId().equals(partId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	
	/**
	 * 包含经纬度
	 * 区域下拉框
	 * @return
	 */
	public Object areaList2() {
		List<Part> list = partDao.listAll();
		List<AreaSelectVo> result = new ArrayList<>();
		for (Part item : list) {
			AreaSelectVo sv = new AreaSelectVo(item.getArea(), item.getName(), item.getPic(), item.getLon(), item.getLat(), item.getContent(), item.getCreateTime());
			result.add(sv);
		}
		return result;
	}
	
	
	/**
	 * 区域下拉框
	 * @return
	 */
	public Object areaList() {
		List<Part> list = partDao.listAll();
		List<SelectVo> result = new ArrayList<>();
		for (Part item : list) {
			SelectVo sv = new SelectVo(item.getArea(), item.getName(), item.getPic());
			result.add(sv);
		}
		return result;
	}
	
	/**
	 * 区域
	 * @return
	 */
	public Object listPart() {
		List<Part> partList = partDao.listAll();
		List<ListVo> result = new ArrayList<>();
		transferList(partList, result);
		return result;
	}
	
	/**
	 * 区域 
	 * @return
	 */
	public Object listPartId() {
		List<Part> partList = partDao.listAll();
		List<Long> result = new ArrayList<>();
		transferListId(partList, result);
		return result;
	}
	
	private void transferListId(List<Part> partList, List<Long> result) {
		for (Part item : partList) {
			result.add(item.getPartId());
		}
	}

	private void transferList(List<Part> partList, List<ListVo> result) {
		for (Part item : partList) {
			ListVo vo = new ListVo();
			vo.setKey(item.getPartId());
			vo.setLabel(item.getName());
			result.add(vo);
		}
		
	}

	/**
	 * 区域列表
	 * @param bo
	 * @return
	 */
	public Object list(SearchPartBo bo) {
		PageVo<ListPartVo> result = new PageVo<ListPartVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(partDao.list(bo));
        result.setTotal(partDao.countList(bo));
		return result;
	}
	
	/**
	 * 区域详情
	 * @param partId
	 * @return
	 * @throws AabException
	 */
	public Object detail(Long partId) throws AabException {
		Object result = partDao.detail(partId);
		if (result == null) {
			throw new AabException(ExceptionCode.数据不存在);
		}
		return result;
	}
	
	

}
